/*     */ package com.zimbra.cs.service.mail;
/*     */ 
/*     */ import com.zimbra.common.calendar.TimeZoneMap;
/*     */ import com.zimbra.common.service.ServiceException;
/*     */ import com.zimbra.common.soap.Element;
/*     */ import com.zimbra.common.util.Log;
/*     */ import com.zimbra.common.util.ZimbraLog;
/*     */ import com.zimbra.cs.account.Account;
/*     */ import com.zimbra.cs.mailbox.CalendarItem;
/*     */ import com.zimbra.cs.mailbox.Folder;
/*     */ import com.zimbra.cs.mailbox.MailServiceException;
/*     */ import com.zimbra.cs.mailbox.Mailbox;
/*     */ import com.zimbra.cs.mailbox.MailboxLock;
/*     */ import com.zimbra.cs.mailbox.OperationContext;
/*     */ import com.zimbra.cs.mailbox.calendar.Invite;
/*     */ import com.zimbra.cs.mailbox.calendar.RecurId;
/*     */ import com.zimbra.cs.mailbox.calendar.ZAttendee;
/*     */ import com.zimbra.cs.service.util.ItemId;
/*     */ import com.zimbra.cs.service.util.ItemIdFormatter;
/*     */ import com.zimbra.soap.ZimbraSoapContext;
/*     */ import java.util.ArrayList;
/*     */ import java.util.List;
/*     */ import java.util.Map;
/*     */ import javax.mail.Address;
/*     */ import javax.mail.MessagingException;
/*     */ import javax.mail.internet.MimeMessage;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class CreateCalendarItemException
/*     */   extends CalendarRequest
/*     */ {
/*     */   protected class CreateCalendarItemExceptionInviteParser
/*     */     extends ParseMimeMessage.InviteParser
/*     */   {
/*     */     private String mUid;
/*     */     private Invite mDefaultInvite;
/*     */     private CalendarRequest.MailSendQueue sendQueue;
/*     */     
/*     */     CreateCalendarItemExceptionInviteParser(String uid, Invite defaultInvite, CalendarRequest.MailSendQueue sendQueue)
/*     */     {
/*  51 */       this.mUid = uid;
/*  52 */       this.mDefaultInvite = defaultInvite;
/*  53 */       this.sendQueue = sendQueue;
/*     */     }
/*     */     
/*     */     public ParseMimeMessage.InviteParserResult parseInviteElement(ZimbraSoapContext zsc, OperationContext octxt, Account account, Element inviteElem)
/*     */       throws ServiceException
/*     */     {
/*  59 */       ParseMimeMessage.InviteParserResult toRet = CalendarUtils.parseInviteForCreateException(account, CreateCalendarItemException.this.getItemType(), inviteElem, this.mDefaultInvite.getTimeZoneMap() != null ? this.mDefaultInvite.getTimeZoneMap().clone() : null, this.mUid, this.mDefaultInvite);
/*     */       
/*     */ 
/*     */ 
/*     */ 
/*  64 */       List<ZAttendee> removedAttendees = CalendarUtils.getRemovedAttendees(this.mDefaultInvite.getAttendees(), toRet.mInvite.getAttendees(), true, account);
/*     */       
/*  66 */       if (removedAttendees.size() > 0) {
/*  67 */         CalendarRequest.notifyRemovedAttendees(zsc, octxt, account, this.mDefaultInvite.getCalendarItem().getMailbox(), this.mDefaultInvite.getCalendarItem(), toRet.mInvite, removedAttendees, this.sendQueue);
/*     */       }
/*     */       
/*     */ 
/*  71 */       return toRet;
/*     */     }
/*     */   }
/*     */   
/*     */   public Element handle(Element request, Map<String, Object> context) throws ServiceException
/*     */   {
/*  77 */     ZimbraSoapContext zsc = getZimbraSoapContext(context);
/*  78 */     Account acct = getRequestedAccount(zsc);
/*  79 */     Mailbox mbox = getRequestedMailbox(zsc);
/*  80 */     OperationContext octxt = getOperationContext(zsc, context);
/*  81 */     ItemIdFormatter ifmt = new ItemIdFormatter(zsc);
/*     */     
/*     */ 
/*     */ 
/*  85 */     Element msgElem = request.getElement("m");
/*  86 */     String folderStr = msgElem.getAttribute("l", null);
/*  87 */     ItemId iid = new ItemId(request.getAttribute("id"), zsc);
/*  88 */     if (!iid.belongsTo(acct))
/*     */     {
/*  90 */       if (folderStr != null)
/*     */       {
/*  92 */         ItemId folderFQ = new ItemId(folderStr, zsc);
/*  93 */         msgElem.addAttribute("l", folderFQ.toString());
/*     */       }
/*  95 */       return proxyRequest(request, context, iid.getAccountId());
/*     */     }
/*     */     
/*     */ 
/*  99 */     boolean isInterMboxMove = false;
/* 100 */     ItemId iidFolder = null;
/* 101 */     if (folderStr != null) {
/* 102 */       iidFolder = new ItemId(folderStr, zsc);
/* 103 */       isInterMboxMove = !iidFolder.belongsTo(mbox);
/*     */     }
/*     */     
/* 106 */     int compNum = (int)request.getAttributeLong("comp");
/*     */     
/* 108 */     CalendarRequest.MailSendQueue sendQueue = new CalendarRequest.MailSendQueue();
/* 109 */     Element response = getResponseElement(zsc);
/* 110 */     mbox.lock.lock();
/*     */     try {
/* 112 */       CalendarItem calItem = mbox.getCalendarItemById(octxt, iid.getId());
/* 113 */       if (calItem == null) {
/* 114 */         throw MailServiceException.NO_SUCH_CALITEM(iid.getId(), " for CreateCalendarItemExceptionRequest(" + iid + "," + compNum + ")");
/*     */       }
/*     */       
/* 117 */       if (calItem.inTrash())
/* 118 */         throw ServiceException.INVALID_REQUEST("cannot modify a calendar item under trash", null);
/* 119 */       if ((!isInterMboxMove) && (iidFolder != null) && 
/* 120 */         (iidFolder.getId() != calItem.getFolderId())) {
/* 121 */         Folder destFolder = mbox.getFolderById(octxt, iidFolder.getId());
/* 122 */         if (destFolder.inTrash()) {
/* 123 */           throw ServiceException.INVALID_REQUEST("cannot combine with a move to trash", null);
/*     */         }
/*     */       }
/*     */       
/*     */ 
/* 128 */       int modSeq = (int)request.getAttributeLong("ms", 0L);
/* 129 */       int revision = (int)request.getAttributeLong("rev", 0L);
/* 130 */       if ((modSeq != 0) && (revision != 0) && ((modSeq < calItem.getModifiedSequence()) || (revision < calItem.getSavedSequence())))
/*     */       {
/* 132 */         throw MailServiceException.INVITE_OUT_OF_DATE(iid.toString());
/*     */       }
/* 134 */       Invite inv = calItem.getInvite(iid.getSubpartId(), compNum);
/* 135 */       if (inv == null)
/* 136 */         throw MailServiceException.INVITE_OUT_OF_DATE(iid.toString());
/* 137 */       if (inv.hasRecurId())
/* 138 */         throw MailServiceException.INVITE_OUT_OF_DATE("Invite id=" + ifmt.formatItemId(iid) + " comp=" + compNum + " is not the default invite");
/* 139 */       if (!calItem.isRecurring()) {
/* 140 */         throw ServiceException.INVALID_REQUEST("CalendarItem " + calItem.getId() + " is not a recurring calendar item", null);
/*     */       }
/* 142 */       CreateCalendarItemExceptionInviteParser parser = new CreateCalendarItemExceptionInviteParser(calItem.getUid(), inv, sendQueue);
/* 143 */       CalendarRequest.CalSendData dat = handleMsgElement(zsc, octxt, msgElem, acct, mbox, parser);
/* 144 */       dat.mDontNotifyAttendees = isInterMboxMove;
/*     */       
/* 146 */       int folderId = calItem.getFolderId();
/* 147 */       if ((!isInterMboxMove) && (iidFolder != null)) {
/* 148 */         folderId = iidFolder.getId();
/*     */       }
/*     */       
/* 151 */       if (!dat.mInvite.hasRecurId()) {
/* 152 */         ZimbraLog.calendar.info("<CreateCalendarItemException> id=%d, folderId=%d, subject=\"%s\", UID=%s", new Object[] { Integer.valueOf(iid.getId()), Integer.valueOf(folderId), dat.mInvite.isPublic() ? dat.mInvite.getName() : "(private)", dat.mInvite.getUid() });
/*     */       }
/*     */       else
/*     */       {
/* 156 */         ZimbraLog.calendar.info("<CreateCalendarItemException> id=%d, folderId=%d, subject=\"%s\", UID=%s, recurId=%s", new Object[] { Integer.valueOf(iid.getId()), Integer.valueOf(folderId), dat.mInvite.isPublic() ? dat.mInvite.getName() : "(private)", dat.mInvite.getUid(), dat.mInvite.getRecurId().getDtZ() });
/*     */       }
/*     */       
/*     */       boolean hasRecipients;
/*     */       try
/*     */       {
/* 162 */         Address[] rcpts = dat.mMm.getAllRecipients();
/* 163 */         hasRecipients = (rcpts != null) && (rcpts.length > 0);
/*     */       } catch (MessagingException e) {
/* 165 */         throw ServiceException.FAILURE("Checking recipients of outgoing msg ", e);
/*     */       }
/*     */       
/* 168 */       if ((!dat.mInvite.isOrganizer()) && (hasRecipients)) {
/* 169 */         throw MailServiceException.MUST_BE_ORGANIZER("CreateCalendarItemException");
/*     */       }
/* 171 */       if (!dat.mInvite.isOrganizer())
/*     */       {
/* 173 */         dat.mInvite.setNeverSent(false);
/* 174 */       } else if (!dat.mInvite.hasOtherAttendees())
/*     */       {
/* 176 */         dat.mInvite.setNeverSent(false);
/* 177 */       } else if (hasRecipients)
/*     */       {
/* 179 */         dat.mInvite.setNeverSent(false);
/*     */ 
/*     */       }
/*     */       else
/*     */       {
/* 184 */         dat.mInvite.setNeverSent(inv.isNeverSent());
/*     */       }
/* 186 */       boolean forceSend = request.getAttributeBool("forcesend", true);
/* 187 */       sendCalendarMessage(zsc, octxt, folderId, acct, mbox, dat, response, true, forceSend, sendQueue);
/* 188 */       boolean echo = request.getAttributeBool("echo", false);
/* 189 */       if ((echo) && (dat.mAddInvData != null)) {
/* 190 */         int maxSize = (int)request.getAttributeLong("max", 0L);
/* 191 */         boolean wantHTML = request.getAttributeBool("html", false);
/* 192 */         boolean neuter = request.getAttributeBool("neuter", true);
/* 193 */         echoAddedInvite(response, ifmt, octxt, mbox, dat.mAddInvData, maxSize, wantHTML, neuter);
/*     */       }
/*     */     } finally {
/* 196 */       mbox.lock.release();
/* 197 */       sendQueue.send();
/*     */     }
/*     */     
/*     */ 
/* 201 */     if (isInterMboxMove) {
/* 202 */       CalendarItem calItem = mbox.getCalendarItemById(octxt, iid.getId());
/* 203 */       List<Integer> ids = new ArrayList(1);
/* 204 */       ids.add(Integer.valueOf(calItem.getId()));
/* 205 */       ItemActionHelper.MOVE(octxt, mbox, zsc.getResponseProtocol(), ids, calItem.getType(), null, iidFolder);
/*     */     }
/*     */     
/* 208 */     return response;
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/service/mail/CreateCalendarItemException.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */